package com.lmx.messagetemplate.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lmx.messagetemplate.R;
import com.lmx.messagetemplate.bean.MessageTemplate;

import java.util.List;

public class TitleAdapterRv extends BaseAdapterRv<MessageTemplate> {

    public TitleAdapterRv(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_template, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MessageTemplate item = getItem(position);
        if (onItemClickListener != null) {
            ((ViewHolder) holder).rootView.setOnClickListener(v -> onItemClickListener.onItemClick(v, item));
        }
        ((ViewHolder) holder).refreshView(item, position);
    }

    public OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {

        void onItemClick(View view, MessageTemplate data);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView tv_title;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        }

        private void refreshView(MessageTemplate template, int position) {
            tv_title.setText(template.getTemplateTitle());
        }

    }
}
